# -*- coding: utf-8 -*-
from datetime import timedelta

from django import forms

from google.appengine.ext.db import djangoforms
from base.models import School, OneClass, ClassProperty
from base.util.decorators import render_to, login_required, admin_required
import logging

@login_required
@render_to('oneclass/list.html')
def list_by_classproperty(request, classproperty_id):
    classproperty = ClassProperty.get_using_id(int(classproperty_id))
    query = OneClass.gql('WHERE classproperty=:1 ORDER BY date ASC', classproperty)
    onesclasses = query.fetch(10)
    return {'onesclasses': onesclasses}

class OneClassForm(djangoforms.ModelForm):
    class Meta:
        model = OneClass
        exclude =('id',)

#    def clean(self):
#        pass
#        cleaned_data = self.cleaned_data
#        name = cleaned_data.get('name')
#        if School.is_existed(name):
#            raise forms.ValidationError(u'You already have the same school\'s name')
#        return cleaned_data

## class
class ClassRangeForm(forms.Form):
    start_date = forms.DateField()
    end_date = forms.DateField()
    option = forms.ChoiceField(choices=[('1', 'every week'), ('2', 'other week')])

@admin_required
@render_to('oneclass/form.html')
def create(request):
    data = request.GET.copy()
    school_id = data.get('school', '').strip()
    if school_id:
        school = School.get_using_id(int(school_id))
        form = OneClassForm(initial={'school': school.key()})
    else:
        form = OneClassForm()
    if request.method == 'POST':
        form = OneClassForm(request.POST)
        if form.is_valid():
            oneclass = form.save(commit=False)
            oneclass.put()
            return oneclass.get_absolute_url
    return {
            'form': form,
            }

@admin_required
@render_to('oneclass/form_in_bulk.html')
def create_in_bulk(request, classproperty_id):
    form = ClassRangeForm()
    if request.method == 'POST':
        form = ClassRangeForm(request.POST)
        if form.is_valid():
            start_date = form.cleaned_data['start_date']
            end_date = form.cleaned_data['end_date']
            option = form.cleaned_data['option']
            next_days = timedelta(7 * int(option))

            classproperty = ClassProperty.get_using_id(int(classproperty_id))
            assigned_day = start_date
            while end_date > assigned_day:
#                oneclass = OneClass(parent=classproperty.school)
                oneclass = OneClass()
                oneclass.date = assigned_day
                oneclass.day = classproperty.day
                oneclass.start_time = classproperty.start_time
                oneclass.end_time = classproperty.end_time
                oneclass.school = classproperty.school
                oneclass.eteacher = classproperty.eteacher
                oneclass.classproperty = classproperty
                oneclass.put()
                assigned_day = assigned_day + next_days
            return classproperty.school.get_absolute_url
    return {'form': form}

@admin_required
@render_to('oneclass/detail.html')
def detail(request, class_id):
    oneclass = OneClass.get_using_id(int(class_id))
    return {'oneclass': oneclass}

@admin_required
@render_to('oneclass/form.html')
def change(request, class_id):
    oneclass = OneClass.get_using_id(int(class_id))
    form = OneClassForm(request.POST or None, instance=oneclass)
    if form.is_valid():
        oneclass = form.save()
        return oneclass.get_absolute_url
    return {'form': form}